using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Reflection;

namespace Pow
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
		SpriteFont spriteFont;
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        Jogador jogador;
        public bool gameover;
		public enum GameState
		{
			Intro,
			Menu,
			Game,
			Credits,
			Pause,
			GameOver,
			Quit
		}


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;

            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
			currentState = GameState.Intro;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
			spriteFont = Content.Load<SpriteFont>("SpriteFont1");
            jogador = new Jogador(this);
        }

        protected override void UnloadContent()
        {
        }


		private KeyboardState oldkeystate;
        private KeyboardState newkeystate;

        protected override void Update(GameTime gameTime)
        {
			newkeystate = Keyboard.GetState();
			switch (currentState) { 
				case GameState.Intro:
				case GameState.Credits:
					if (newkeystate.IsKeyDown(Keys.Enter) && !oldkeystate.IsKeyDown(Keys.Enter))
						currentState = Game1.GameState.Menu;
					break;

				case GameState.Menu:
					if (newkeystate.IsKeyDown(Keys.Enter) && !oldkeystate.IsKeyDown(Keys.Enter))
					{
						switch ((stateHolder as MenuState).selectedButtonIndex)
						{
							case 0:
								currentState = Game1.GameState.Game;
								break;
							case 1:
								currentState = Game1.GameState.Credits;
								break;
							case 2:
								currentState = Game1.GameState.Quit;
								Exit();
								break;
						}
					}
					break;
                case GameState.Game:
                    CheckJogador();
                    
                    break;

			}

			oldkeystate = newkeystate;
            base.Update(gameTime);
        }

        public void CheckJogador ()
        {
            if (newkeystate.IsKeyDown(Keys.Enter) && !oldkeystate.IsKeyDown(Keys.Enter) && gameover)
                        currentState = Game1.GameState.Menu;
        }
		private GameState _currentState;
		private GameComponent stateHolder;
		public GameState currentState
		{
			get { return _currentState; }
			set {
				Console.WriteLine("currentstate "+value);
				Components.Clear();
				switch (value) { 
					case GameState.Intro:
						Components.Add(stateHolder = new IntroState(this));
						break;
					case GameState.Menu:
						Components.Add(stateHolder = new MenuState(this));
						break;
					case GameState.Game:
						Components.Add(stateHolder = new GameplayState(this));
						break;
					case GameState.Credits:
						Components.Add(stateHolder = new CreditsState(this));
						break;
				}
				_currentState = value;
			}
		}



		protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
			/*
            spriteBatch.Begin();
			spriteBatch.DrawString(spriteFont, "aaaa", Vector2.Zero, Color.Black);
            spriteBatch.End();
            */
            base.Draw(gameTime);
        }
    }
}
